import 'package:dulu/config/global.dart';
import 'package:dulu/iconfont.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';

class Header extends StatefulWidget {
  Header({Key key, this.onDateChange}) : super(key: key);
  final Function onDateChange;
  @override
  _HeaderState createState() => _HeaderState();
}

class _HeaderState extends State<Header> {
  int year = 2021;
  int month = 1;

  @override
  void initState() {
    year = DateTime.now().year;
    month = DateTime.now().month;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(0, Global.padding.top, 0, 20),
      child: Row(
        children: [
          SizedBox(width: 15),
          InkWell(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: Container(
              width: 40,
              child: Icon(IconFont.close),
            ),
          ),
          Expanded(
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    InkWell(
                      onTap: () {
                        DatePicker.showDatePicker(
                          context,
                          pickerTheme: DateTimePickerTheme(confirm: Text('确定')),
                          onMonthChangeStartWithFirstDate: true,
                          minDateTime: DateTime.parse('2021-01-01'),
                          maxDateTime: DateTime.now(),
                          initialDateTime: DateTime.now(),
                          dateFormat: 'yyyy-MMMM',
                          locale: DateTimePickerLocale.zh_cn,
                          onConfirm: (dateTime, List<int> index) {
                            widget.onDateChange(dateTime);
                            setState(() {
                              year = dateTime.year;
                              month = dateTime.month;
                            });
                          },
                        );
                      },
                      child: Text(
                        '$year  $month月',
                        style: TextStyle(
                          color: Global.fontColor,
                          fontSize: 20,
                          fontFamily: 'Arciform',
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      color: Global.fontColor,
                      size: 14,
                    ),
                  ],
                ),
                Text(
                  '月份总览',
                  style: TextStyle(
                    color: Global.fontColor.withOpacity(.5),
                    fontSize: 12,
                    fontFamily: 'Arciform',
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),
          ),
          SizedBox(width: 50),
        ],
      ),
    );
  }
}
